//
//  IconDetailViewController.m
//  FNer
//
//  Created by 王展 on 15/10/7.
//  Copyright (c) 2015年 wangzhan. All rights reserved.
//

#import "IconDetailViewController.h"
#import "AFNetworking.h"
#import "DetailHtworld.h"
#import "UIActivityIndicatorView+AFNetworking.h"

@interface IconDetailViewController ()

@property(nonatomic,retain)NSMutableArray *htWorldArray;
@property(nonatomic,retain)UIScrollView *bigSV;
@property(nonatomic,retain)UILabel *titleLabel;
@property(nonatomic,retain)UIActivityIndicatorView *activity;

@end

@implementation IconDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
    self.titleLabel.text = @"详情";
    self.titleLabel.font = [UIFont systemFontOfSize:17 * XWIDTH];
    [self.titleLabel setTextAlignment:NSTextAlignmentCenter];
    self.titleLabel.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = self.titleLabel;
    
    NSString *string = [[NSUserDefaults standardUserDefaults] objectForKey:@"nighting"];
    if ([string isEqualToString:@"yes"]) {
#warning 改的颜色
        self.titleLabel.textColor = [UIColor blackColor];
    }
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColoring:) name:@"changeColoring" object:nil];
    
    UIImage *leftImage = [UIImage imageNamed:@"导航栏_button_back_用于白和灰.png"];
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction)];
    
    [self getIconDetailData];
    
    [self setBigScrollView];
}

-(void)leftButtonAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

//建立大UIScrollView
-(void)setBigScrollView
{
    self.bigSV = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.bigSV.backgroundColor = [UIColor whiteColor];
    self.bigSV.delegate = self;
    [self.view addSubview:self.bigSV];
    
    self.bigSV.bounces = NO;
    self.bigSV.pagingEnabled = YES;
    self.bigSV.contentOffset = CGPointMake(WIDTH * self.page, 0);
    
    //创建小菊花
    _activity = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    _activity.center =self.view.center;
    [self.view addSubview:_activity];
}


//获取详情数据
-(void)getIconDetailData
{
    
    NSString *url_string = [NSString stringWithFormat:@"http://api.imzhitu.com/ztworld/ztworld_queryMyWorld?userId=%ld&joinId=%ld&sinceId=0&maxId=0&start=1&limit=21&trimExtras=false&commentLimit=2&likedLimit=10",self.auser.id,self.auser.id];

    AFNetworkReachabilityManager *Netmanger = [AFNetworkReachabilityManager sharedManager];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager.requestSerializer setValue:@"IMZHITU_REMEMBER_ME_COOKIE=QjFuK0Nmc1RleEl4VTVvSW02a0R4UT09OkJreEI0c1BuYkI4YWRKV3JocitTd0E9PQ; JSESSIONID=444A1130F5376C095EF2207F505A7B0B; SERVERID=f2bba00007611e8b21e74f5166ce6604|1444698911|1444696238" forHTTPHeaderField:@"Cookie"];

    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    
    [manager GET:url_string parameters:nil success:^(NSURLSessionDataTask *operation, id responseObject) {
        [Netmanger stopMonitoring];
        self.htWorldArray = [NSMutableArray array];
        NSMutableDictionary *bigDic = responseObject;
        NSMutableArray *bigArray = [bigDic objectForKey:@"htworld"];
        for (NSMutableDictionary *dic in bigArray) {
            DetailHtworld *ahtWorld = [[DetailHtworld alloc] init];
            [ahtWorld setValuesForKeysWithDictionary:dic];
            [self.htWorldArray addObject:ahtWorld];
        }
      
        self.bigSV.contentSize = CGSizeMake(WIDTH * self.htWorldArray.count, HIGHT);
        for (int i = 0; i < self.htWorldArray.count; i++) {
            UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, HIGHT - 64)];
            webView.backgroundColor = [UIColor blackColor];
            webView.delegate = self;
            [webView setScalesPageToFit:YES];
            DetailHtworld *ahtWorld = [[DetailHtworld alloc] init];
            ahtWorld = [self.htWorldArray objectAtIndex:i];
            NSString *wangZhi = ahtWorld.worldURL;
            NSURL *url = [NSURL URLWithString:wangZhi];
            NSURLRequest *requset = [NSURLRequest requestWithURL:url];
            [webView loadRequest:requset];
            [self.bigSV addSubview:webView];
            
            
        }
    }failure:^(NSURLSessionDataTask *operation, NSError *error) {
       
    }];
}


-(void)changeColoring:(NSNotification *)notification
{
#warning 改的颜色
    self.titleLabel.textColor = [UIColor blackColor];
    
    NSString *string = [notification object];
    if ([string isEqualToString:@"no"]) {
        
#warning 原有颜色
        self.titleLabel.textColor = [UIColor whiteColor];
    }
    //保存当前状态值
    [[NSUserDefaults standardUserDefaults] setObject:string forKey:@"nighting"];
}

-(void)webViewDidStartLoad:(UIWebView *)webView
{
    [_activity startAnimating];
    
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    [_activity stopAnimating];
    
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [_activity stopAnimating];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
